/*
 * CustomerDisplayPurchaseInfo.java
 *
 * Created on Jan 15, 2011, 4:10:40 PM
 */
package edu.rit.varapp.graphical;

import edu.rit.varapp.datalayer.Customer;
import edu.rit.varapp.datalayer.CustomerOps;
import edu.rit.varapp.datalayer.Module;
import edu.rit.varapp.datalayer.ModuleOps;
import edu.rit.varapp.utilities.ExceptionUtils;
import java.awt.Dimension;
import java.awt.Font;
import java.sql.SQLException;
import java.util.HashMap;

/**
 *
 * @author Eric Kisner
 */
public class CustomerDisplayPurchaseInfo extends AbstractView {

    private final CustomerOps cOps = new CustomerOps();
    private final ModuleOps mOps = new ModuleOps();
    private int entityId;
    private static final int VIEW_WIDTH = 569;
    private static final int VIEW_HEIGHT = 481;

    /** Creates new form CustomerDisplayPurchaseInfo */
    public CustomerDisplayPurchaseInfo( int entityId ) {
        super( new Dimension( VIEW_WIDTH, VIEW_HEIGHT ) );
        this.entityId = entityId;
        initComponents();
        displayCustomerData();
    }

    /**
     * Display this customer's data (their contact information, the license codes, and Tech Support Exp dates of
     * any software purchases).
     */
    private void displayCustomerData() {
        Customer customer = null;
        customerInfo.setFont( new Font( "Serif", Font.BOLD, 14 ) );
        purchasesInfo.setFont( new Font( "Serif", Font.BOLD, 14 ) );
        try {
            //Retrieve the customer information, and display any information that was not empty.
            customer = cOps.retrieveCustomer( entityId );
            customerInfo.setText( getCustomerString( customer ) );

            //Retrieve all purchases for this customer, and display only their license codes and Tech. supp. expiration
            for( Module module : mOps.retrieveModules( customer ) ) {
                purchasesInfo.append( getModuleString( module ) );
                purchasesInfo.append( "\n" );
            }
            purchasesInfo.setCaretPosition( 0 );
        } catch( SQLException sqle ) {
            ExceptionUtils.handleException( this, sqle, "There was a problem trying to retrieve your information."
                                                        + "\nPlease ask an administrator to check the log files for "
                                                        + "more details.", this.getClass() );
            System.exit( 1 );
        }
    }

    /**
     * Gets the Customer's contact information as a String.
     */
    private String getCustomerString( Customer customer ) {
        StringBuilder buffer = new StringBuilder();
        HashMap<String, String> customerMap = customer.niceMap( false );
        for( String key : customerMap.keySet() ) {
            buffer.append( key );
            buffer.append( ":\t" );
            buffer.append( customerMap.get( key ) );
            buffer.append( "\n" );

        }
        return buffer.toString().trim();
    }

    /**
     * Gets the License Code and Tech Support Expiration Date of a purchase made by this Customer.
     */
    private String getModuleString( Module module ) {
        final String licenseCode = "License Code: ";
        final String techSuppExp = "TechSupport Expiration: ";
        StringBuilder buffer = new StringBuilder();
        buffer.append( licenseCode );
        buffer.append( "\t" );
        buffer.append( module.getLicenseCode() );
        buffer.append( "\n" );
        buffer.append( techSuppExp );
        buffer.append( "\t" );
        buffer.append( module.getSupportExpDate() );
        buffer.append( "\n" );
        return buffer.toString();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings( "unchecked" )
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        customerInfo = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        purchasesInfo = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Purchase Information");
        setMinimumSize(new java.awt.Dimension(569, 481));
        setPreferredSize(new java.awt.Dimension(569, 481));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        getContentPane().setLayout(null);

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        getContentPane().add(backButton);
        backButton.setBounds(200, 420, 140, 29);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Customer Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        customerInfo.setColumns(20);
        customerInfo.setEditable(false);
        customerInfo.setLineWrap(true);
        customerInfo.setRows(2);
        customerInfo.setWrapStyleWord(true);
        jScrollPane2.setViewportView(customerInfo);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(20, 10, 510, 180);

        jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Purchase Information", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        purchasesInfo.setColumns(20);
        purchasesInfo.setEditable(false);
        purchasesInfo.setLineWrap(true);
        purchasesInfo.setRows(5);
        purchasesInfo.setWrapStyleWord(true);
        purchasesInfo.setCaretPosition(0);
        purchasesInfo.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(purchasesInfo);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 200, 510, 210);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        new CustomerOperations( entityId ).setVisible( true );
    }//GEN-LAST:event_formWindowClosed

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_backButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JTextArea customerInfo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea purchasesInfo;
    // End of variables declaration//GEN-END:variables
}
